package com.qingke.easyjava;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qingke.easyjava.mybatis.pojo.MyClass;
import com.qingke.easyjava.mybatis.pojo.Student;
import com.qingke.easyjava.mybatis.pojo.User;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {    
    	//mybatis的配置文件
        String resource ="mybatis_conf.xml";
        //使用类加载器加载mybaties的配置文件（它也加载关联的映射文件）
        InputStream is =App.class.getClassLoader().getResourceAsStream(resource);
        //构造sqlSession的工厂
        SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(is);
        //创建能执行映射文件中sql的sqlSession
        SqlSession session=sessionFactory.openSession();
//        
//        String statement="com.qingke.easyjava.mybatis.mapping.userMapper.getAllUsers";
//        List<User> users =session.selectList(statement);
//        for(User user:users){
//        	System.out.println(user);
//        }
//        statement ="com.qingke.easyjava.mybatis.mapping.studentMapper.getStudentById";
//        Student stduent =session.selectOne(statement,1);
//        System.out.println("getStudentById："+stduent);
//        
//        userHashMap(session);
//        
//        User u =new User();
//        u.setCode("x5");
//        u.setName("哈尼");
//        u.setPassword("5555");
//        addNewUser(session,u);
//        session.commit();
          Student search =new Student();
          getStduentNameLike(session,search);
          search.setName("");
          System.out.println("======================");
          getStduentNameLike(session,search);
          System.out.println("======================");
          search.setName("%城%");
          getStduentNameLike(session,search);
          

          List<Integer> list =new ArrayList<Integer>();
          for(int i=1;i<8;i++){
        	  list.add(i);
          }
          forEachStudent(session, list);
          
          callSP(session);
          
        
    }
    
    public static void userHashMap(SqlSession session){
    	String statement ="com.qingke.easyjava.mybatis.mapping.userMapper.getUserAttrMapById";
    	Map<String ,Object> user =session.selectOne(statement,3);
    	for(String key :user.keySet()){
    		System.out.println("key="+key+",value="+user.get(key)+",vt="+user.get(key).getClass().getSimpleName());
    	}
    }
    public static void addNewUser(SqlSession session,User user){
    	String statement ="com.qingke.easyjava.mybatis.mapping.userMapper.insertUser";
    	System.out.println("before="+user);
    	int count =session.insert(statement,user);
    	System.out.println("insert count="+count);
    	System.out.println("after="+user);
    }
    public static void updateUser(SqlSession session,User user){
    	String statement ="com.qingke.easyjava.mybatis.mapping.userMapper.updateUser";
    	
    }
    public static void deleteUser(SqlSession session,User user){
    	String statement ="com.qingke.easyjava.mybatis.mapping.userMapper.deleteUser";
    	
    }
    public static void getStudent(SqlSession session){
    	String  st="com.qingke.easyjava.mybatis.mapping.studentMapper.getStduentById";
    	Student student =session.selectOne(st,2);
    	System.out.println(student);
    	System.out.println(student.getClazz());
    }
    public static void getMyClass(SqlSession session){
    	String  st="com.qingke.easyjava.mybatis.mapping.classMapper.getClassById";
    	MyClass mc =session.selectOne(st, 1);
    	System.out.println(mc);
    	for(Student stu:mc.getStudents()){
    		System.out.println(stu);
    	}

    }
    public static void getStduentNameLike(SqlSession session ,Student search){
    	String st="com.qingke.easyjava.mybatis.mapping.studentMapper.getStduentNameLike";
    	List<Student> students =session.selectList(st,search);
    	for(Student student:students){
    		System.out.println(student);
    	}
    }
    public static void forEachStudent(SqlSession session,List list){
    	String st="com.qingke.easyjava.mybatis.mapping.studentMapper.selectStudentIn";
    	List<Student> students =session.selectList(st,list);
    	for(Student student :students){
    		System.out.println(student);
    	}
    }
   public static void callSP(SqlSession session){
	   String str ="com.qingke.easyjava.mybatis.mapping.studentMapper.callSPtest";
	   Map<String,Integer> map=new HashMap<String,Integer>();
	   map.put("addend1", 6);
	   map.put("addend2", 7);
	   
	   session.update(str,map);
	   System.out.println(map.get("theSum"));
   }
    
    
}
